package cn.edu.jxnu.leetcode.scala

/**
 * 对角元素相等的矩阵
 *
 * 766. Toeplitz Matrix (Easy)
 *
 * 1234
 * 5123
 * 9512
 *
 * In the above grid, the diagonals are "[9]", "[5, 5]", "[1, 1, 1]", "[2, 2, 2]", "[3, 3]", "[4]",
 * and in each diagonal all elements are the same, so the answer is True.
 * @author 梦境迷离
 * @time 2018年7月23日
 * @version v1.0
 */
object Leetcode_766_Array extends App {

    val n1 = Array(1, 2, 3, 4)
    val n2 = Array(5, 1, 2, 3)
    val n3 = Array(9, 5, 1, 2)
    val nums = Array(n1, n2, n3)
    print(isToeplitzMatrix(nums))

    def isToeplitzMatrix(matrix: Array[Array[Int]]): Boolean = {
        for (i ← 0 until matrix(0).length) {
            if (!check(matrix, matrix(0)(i), 0, i)) {
                return false
            }
        }

        for (i ← 0 until matrix.length) {
            if (!check(matrix, matrix(i)(0), i, 0)) {
                return false
            }
        }

        true

    }
    def check(matrix: Array[Array[Int]], expectValue: Int, row: Int, col: Int): Boolean = {
        if (row >= matrix.length || col >= matrix(0).length) {
            return true
        }
        if (matrix(row)(col) != expectValue) {
            return false
        }
        check(matrix, expectValue, row + 1, col + 1)
    }

}